package com.atguigu.srb.core.service.impl;

import com.atguigu.srb.core.enums.BorrowerStatusEnum;
import com.atguigu.srb.core.enums.IntegralEnum;
import com.atguigu.srb.core.mapper.BorrowerAttachMapper;

import com.atguigu.srb.core.mapper.UserInfoMapper;
import com.atguigu.srb.core.mapper.UserIntegralMapper;
import com.atguigu.srb.core.pojo.entity.Borrower;
import com.atguigu.srb.core.mapper.BorrowerMapper;


import com.atguigu.srb.core.pojo.entity.UserInfo;
import com.atguigu.srb.core.pojo.entity.UserIntegral;
import com.atguigu.srb.core.pojo.vo.BorrowerApprovalVO;
import com.atguigu.srb.core.pojo.vo.BorrowerDetailVO;
import com.atguigu.srb.core.pojo.vo.BorrowerVO;
import com.atguigu.srb.core.service.BorrowerAttachService;
import com.atguigu.srb.core.service.BorrowerService;
import com.atguigu.srb.core.service.DictService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 借款人 服务实现类
 * </p>
 *
 * @author Bright
 * @since 2022-04-09
 */
@Service
@Slf4j
public class BorrowerServiceImpl extends ServiceImpl<BorrowerMapper, Borrower> implements BorrowerService {

    @Resource
    UserInfoMapper userInfoMapper;

    @Resource
    BorrowerAttachMapper borrowerAttachMapper;

    @Resource
    DictService dictService;

    @Resource
    BorrowerAttachService borrowerAttachService;

    @Resource
    UserIntegralMapper userIntegralMapper;

    @Override
    public Integer getBorrorStatusByUserId(Long userId) {
        QueryWrapper<UserInfo> userInfoQueryWrapper = new QueryWrapper<>();
        userInfoQueryWrapper.select("borrow_auth_status").eq("id", userId);
        List<Object> objects = userInfoMapper.selectObjs(userInfoQueryWrapper);

        return (Integer) objects.get(0);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void saveByVo(BorrowerVO borrowerVO, Long userId) {
        UserInfo userInfo = userInfoMapper.selectById(userId);
        Borrower borrower = new Borrower();
        BeanUtils.copyProperties(borrowerVO, borrower);
        borrower.setUserId(userId);
        borrower.setName(userInfo.getName());
        borrower.setIdCard(userInfo.getIdCard());
        borrower.setMobile(userInfo.getMobile());
        borrower.setStatus(BorrowerStatusEnum.AUTH_RUN.getStatus());

        baseMapper.insert(borrower);
        log.info("存之后borrower_id:" + borrower.getUserId().toString());
        //认证中

        borrowerVO.getBorrowerAttachList().forEach(borrowerAttach -> {
            borrowerAttach.setBorrowerId(borrower.getId());
            borrowerAttachMapper.insert(borrowerAttach);
        });
        userInfo.setBorrowAuthStatus(BorrowerStatusEnum.AUTH_RUN.getStatus());
        userInfoMapper.updateById(userInfo);

    }

    @Override
    public IPage<Borrower> getPageList(Page<Borrower> pageParam, String keyword) {
        QueryWrapper<Borrower> borrowerQueryWrapper = new QueryWrapper<>();
        if (!StringUtils.isBlank(keyword)) {
            borrowerQueryWrapper
                    .like("name", keyword)
                    .or().like("id_card", keyword)
                    .or().like("mobile", keyword);
        }

        return baseMapper.selectPage(pageParam, borrowerQueryWrapper);
    }

    @Override
    public BorrowerDetailVO getBorrowerVoById(Long id) {
        Borrower borrower = baseMapper.selectById(id);
        BorrowerDetailVO borrowerDetailVO = new BorrowerDetailVO();
        BeanUtils.copyProperties(borrower, borrowerDetailVO);
        borrowerDetailVO.setMarry(borrower.getMarry() ? "是" : "否");
        borrowerDetailVO.setSex(borrower.getSex() == 1 ? "男" : "女");

        borrowerDetailVO.setEducation(dictService.getDictNamebyValueAndDictCode(borrower.getEducation(), "education"));
        borrowerDetailVO.setIncome(dictService.getDictNamebyValueAndDictCode(borrower.getIncome(), "income"));
        borrowerDetailVO.setIndustry(dictService.getDictNamebyValueAndDictCode(borrower.getIndustry(), "industry"));
        borrowerDetailVO.setContactsRelation(dictService.getDictNamebyValueAndDictCode(borrower.getContactsRelation(), "relation"));
        borrowerDetailVO.setReturnSource(dictService.getDictNamebyValueAndDictCode(borrower.getReturnSource(), "returnSource"));
        borrowerDetailVO.setStatus(BorrowerStatusEnum.getMsgByStatus(borrower.getStatus()));

        borrowerDetailVO.setBorrowerAttachVOList(borrowerAttachService.selectBorrowerAttachVOListByBorrowerId(borrower.getId()));


        return borrowerDetailVO;
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void approval(BorrowerApprovalVO borrowerApprovalVO) {
        //修改借款人认证状态
        Borrower borrower = baseMapper.selectById(borrowerApprovalVO.getBorrowerId());
        borrower.setStatus(borrowerApprovalVO.getStatus());
        baseMapper.updateById(borrower);

        UserInfo userInfo = userInfoMapper.selectById(borrower.getUserId());
        userInfo.setBorrowAuthStatus(borrowerApprovalVO.getStatus());
        Integer integerCount = userInfo.getIntegral();
        //添加积分
        UserIntegral userIntegral = new UserIntegral();
        userIntegral.setIntegral(borrowerApprovalVO.getInfoIntegral());
        userIntegral.setUserId(borrower.getUserId());
        userIntegral.setContent("借款人基本信息");
        userIntegralMapper.insert(userIntegral);
        integerCount += borrowerApprovalVO.getInfoIntegral();

        if (borrowerApprovalVO.getIsIdCardOk()) {
            userIntegral = new UserIntegral();
            userIntegral.setIntegral(IntegralEnum.BORROWER_IDCARD.getIntegral());
            userIntegral.setUserId(borrower.getUserId());
            userIntegral.setContent(IntegralEnum.BORROWER_IDCARD.getMsg());
            userIntegralMapper.insert(userIntegral);
            integerCount += IntegralEnum.BORROWER_IDCARD.getIntegral();
        }
        if (borrowerApprovalVO.getIsCarOk()) {
            userIntegral = new UserIntegral();
            userIntegral.setIntegral(IntegralEnum.BORROWER_CAR.getIntegral());
            userIntegral.setUserId(borrower.getUserId());
            userIntegral.setContent(IntegralEnum.BORROWER_CAR.getMsg());
            userIntegralMapper.insert(userIntegral);
            integerCount += IntegralEnum.BORROWER_CAR.getIntegral();
        }
        if (borrowerApprovalVO.getIsHouseOk()) {
            userIntegral = new UserIntegral();
            userIntegral.setIntegral(IntegralEnum.BORROWER_HOUSE.getIntegral());
            userIntegral.setUserId(borrower.getUserId());
            userIntegral.setContent(IntegralEnum.BORROWER_HOUSE.getMsg());
            userIntegralMapper.insert(userIntegral);
            integerCount += IntegralEnum.BORROWER_HOUSE.getIntegral();
        }

        //修改用户信息审核状态和积分
        userInfo.setIntegral(integerCount);
        userInfoMapper.updateById(userInfo);
    }


}
